#ifndef _RPC_SERVER_H_
#define _RPC_SERVER_H_

#ifdef __cplusplus
extern "C"{
#endif

#include "../inc/thread.h"

extern struct thread_master_rpc *master_rpc;

union sockunion {
	struct sockaddr sa;
  	struct sockaddr_in sin;
};

#define RPC_REQUEST 1
#define RPC_RESPONSE 2

#define RPC_SERV_PORT 3369

#define RPC_READ_BUF   2048 /*1024*/

#define RPC_SERV 1
#define RPC_READ 2
#define RPC_WRITE 3

typedef struct _RPC_HEAD_SERV_{
	unsigned int version:8;
    	unsigned int pkt_type:8;
    	unsigned int pkt_len:16;

    	unsigned int resv;

    	unsigned int rpc_id:16;
    	unsigned int param_num:16;
}RPC_HEAD_SERV_ST;

typedef struct _RPC_PARAMS_SERV_{
    	unsigned int type:16;
    	unsigned int length:16;
    	unsigned int value[0];
}RPC_PARAMS_SERV_ST;

enum RPC_TYPE_SERV{
    	TypeServInvalid,
    	TypeServI8,
    	TypeServI16,
    	TypeServI32,
    	TypeServI64,
    	TypeServU8, 
    	TypeServU16,
    	TypeServU32,
    	TypeServU64,
    	TypeServDouble,
    	TypeServString
};

extern int		cc_rpc_get_pkt_len(unsigned char *packet);
extern int 	cc_rpc_get_rpc_id(unsigned char *packet);
extern int 	cc_rpc_get_param_num(unsigned char *packet);
extern int 	cc_rpc_parse_request(unsigned char *request);
extern void 	cc_rpc_parse_param(unsigned char *request, int *offset, unsigned char *result);
extern int 	cc_rpc_generate_response(unsigned char *result, int len);
extern int 	readRpcRequest(int rpcSock,unsigned char *request);
extern int 	ExcuteMethod(unsigned char * request,unsigned char *result,int *return_param_num, int *return_bytes);
extern int 	cc_cc_rpc_generate_head(unsigned char *result,int rpc_id,int return_param_num,int return_bytes);
extern int 	sendResponse(int rpcSock,unsigned char * response,int responseLen);
extern void 	cc_rpc_generate_param(unsigned char *respons, int *offset, int *pkt_len, int type, int len, unsigned char *value);
extern int 	rpc_sockunion_stream_socket (int libIndex, union sockunion *su);
extern int 	rpc_sockunion_bind (int libIndex, int sock, union sockunion *su, unsigned short port, 
				union sockunion *su_addr);
extern int 	rpc_sockopt_reuseaddr (int libIndex, int sock);
extern int 	rpc_sockopt_nodelay(int sock);
extern int 	rpc_sockopt_reuseport (int libIndex, int sock);
extern int 	rpc_sockunion_accept (int libIndex, int sock, union sockunion *su);
extern int 	rpc_accept (int libIndex, struct thread_rpc *thread);
extern int 	rpc_read (int libIndex, struct thread_rpc *thread);
extern int 	rpc_write (int libIndex, struct thread_rpc *thread);
extern void 	rpc_event (int libIndex, int event, int sock, char *vty);
extern void 	rpc_serv_sock (int libIndex, unsigned short port, int family);

extern void *rpc_task();
extern void 	rpc_task_create();


#ifdef __cplusplus
}
#endif
#endif